// pages/shopStore/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '0',
      'title': '海螺号工蜂',
      'color': true,
      'class': false
    },
    imagesURL: app.imagesURL,
    pageHeight: app.globalData.pageHeight, //手机屏幕高度
    navHeight: app.globalData.navHeight,
    topfixed: 0,
    commodityValue: '输入商品名称',
    shopValue: '输入店铺名称',
    salesmanId: null,
    shopStoreInfo: {
      countDealer: 0,
      waitSettlementAmount: 0
    }, //店铺库存汇总
    shopform: {
      dealerName: '', // 店铺名称
      offset: 1,
      type:'1',
      pageSize: 15,
    },
    searchValue: '', //搜索
    stockform: {
      goodsName: '',
      offset: 1,
      pageSize: 15,
    },
    shopInfo: [], //店铺管理
    stockInfo: [], //店铺管理
    scrollTop1: 0,
    manageItem: '1'  //1-店铺  2-库存
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        //区分搜索店铺还是商品
        if(wx.getStorageSync('makeType') == 1){
          that.setData({
            salesmanId: res.data.id,
            'shopform.dealerName': wx.getStorageSync('name'),
            manageItem: 1,
            searchValue: wx.getStorageSync('searchValue'),
            shopValue: wx.getStorageSync('name'),
            shopInfo: []
          })
          if(that.data.shopform.type == 1){
            that.getShopManage()
          }else {
            that.getDacklogShopList()
          }
          
        }else if(wx.getStorageSync('makeType') == 2){
          that.setData({
            salesmanId: res.data.id,
            searchValue: wx.getStorageSync('searchValue'),
            'stockform.goodsName': wx.getStorageSync('name'),
            manageItem: 2,
            commodityValue: wx.getStorageSync('name'),
            stockInfo: []
          })
          that.getStockManage()
        }else {
          that.setData({
            salesmanId: res.data.id,
            commodityValue: '输入商品名称',
            shopValue: '输入店铺名称'
          })
          that.getShopManage()
        }
        //店铺与库存数据汇总
        that.getShopStore()
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    wx.removeStorageSync('makeType')
    wx.removeStorageSync('searchValue')
    wx.removeStorageSync('name')
  },
  // 发送给朋友
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: '海螺号工蜂',
      path: 'pages/logs/logs'
    }
  },
  /* 
    方法
  */
 //跳转库存管理
  handle(e){
    this.setData({
      manageItem: e.currentTarget.dataset['index'],
      'stockform.offset': 1,
      'shopform.offset': 1,
      'shopform.dealerName': '',
      'stockform.goodsName': '',
      commodityValue: '输入商品名称',
      shopValue: '输入店铺名称'
    })
    if(e.currentTarget.dataset['index'] == 1){
      this.getShopManage()
    }else {
      this.getStockManage()
    }
  },
  handleToggleDealer(e){
    this.setData({
        'shopform.type':e.currentTarget.dataset.type,
        'shopform.offset':1,
        scrollTop1: 0,
        shopInfo: []
    })
    if(e.currentTarget.dataset.type == 1){//全部
        this.getShopManage()
    }else { //代办
        this.getDacklogShopList()
    }
  },
  //代办
  getDacklogShopList(){
    var that = this;
    // /insertDealerInfo/findStoreManagements/
    app.baseGet(app.U({
      m: 'bdservice',
      c: 'insertDealerInfo',
      a: 'findStoreManagements/',
      q: {
        salesmanId: that.data.salesmanId, //业务员id
        dealerName: that.data.shopform.dealerName,
        offset: that.data.shopform.offset,
        pageSize: that.data.shopform.pageSize
      }
    }), res => {}, (res) => {
      var data = res.data.list
      if(that.data.offset != 1){
        data = that.data.shopInfo.concat(data)
      }
      that.setData({
        shopInfo: data
      })
    })
  },
  /* 滚动到底部加载 */
  lower(e){
    if(this.data.manageItem == 1 ){
      this.setData({
        'shopform.offset': this.data.shopform.offset + 1
      })
      if(this.data.shopform.type == 1){//全部
        this.getShopManage()
    }else { //代办
        this.getDacklogShopList()
    }
    }else {
      this.setData({
        'stockform.offset': this.data.stockform.offset + 1
      })
      this.getStockManage()
    }
  },
  /* 跳转搜搜 */
  handleSearch(){
    wx.navigateTo({
      url: "/pages/shopStore/search/index?manageItem="+ this.data.manageItem
    })
  },
  /* 跳转店铺详情 */
  handleShopDetail(e){
    wx.navigateTo({
      url: "/pages/shopStore/shopDetail/index?detail=" + JSON.stringify(e.currentTarget.dataset.info)
    })
  },
  /* 跳转库存详情 */
  handleStockDetail(e){
    wx.navigateTo({
      url: "/pages/shopStore/stockDetail/index?detail=" + JSON.stringify(e.currentTarget.dataset.info)
    })
  },
  // 店铺与库存数据汇总
  getShopStore(){
    var that = this;
    app.baseGet(app.U({
      m: 'bdservice',
      c: 'service',
      a: 'findStoresAndInventory/',
      q: {
        salesmanId: that.data.salesmanId, //业务员id
     
      }
    }), res => {}, (res) => {
      that.setData({
        shopStoreInfo: res.data[0]
      })
    })
  },
  //店铺管理
  getShopManage(){
    var that = this;
    app.baseGet(app.U({
      m: 'bdservice',
      c: 'service',
      a: 'findStoreManagement/',
      q: {
        salesmanId: that.data.salesmanId, //业务员id
        dealerName: that.data.shopform.dealerName,
        offset: that.data.shopform.offset,
        pageSize: that.data.shopform.pageSize
      }
    }), res => {}, (res) => {
      var data = res.data.list;
      if(that.data.shopform.offset != 1){
        data = that.data.shopInfo.concat(data)
      }
      that.setData({
        shopInfo: data
      })
    })
  },
  //库存管理
  getStockManage(){
    var that = this;
    app.baseGet(app.U({
      m: 'bdservice',
      c: 'service',
      a: 'findStockInfo/',
      q: {
        salesmanId: that.data.salesmanId, //业务员id
        goodsName: that.data.stockform.goodsName,
        offset: that.data.stockform.offset,
        pageSize: that.data.stockform.pageSize
      }
    }), res => {}, (res) => {
      var data = res.data
      if(that.data.offset != 1){
        data = that.data.stockInfo.concat(data)
      }
      that.setData({
        stockInfo: res.data
      })
    })
  },
  /* 
    滚动监听
  */
 onPageScroll: function(e){
    if(e.scrollTop > 132){
      this.setData({
        topfixed: 134
      })
    }else {
      this.setData({
        topfixed: 0
      })
    }
 },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.removeStorageSync('makeType')
    wx.removeStorageSync('searchValue')
    wx.removeStorageSync('name')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})